'use strict';

const Controller = require('egg').Controller;
const ErrorUtil = require('../../class/error')
module.exports = app => {
  return class FrontIndexController extends Controller {

    /**
     * @description 首页
     * @author xiezuobing[948466]<435321508@qq.com>
     * @memberof HomeController
     */
    async index() {
      const ctx = this.ctx;
      //获取banner
      const banners = await this.service.banner.all();
      //公告取6条，发布时间排序
      const announcements = await this.service.index.getIndexData(app.config.indexModuleName[0], 6);
      //华渔定见取1条，发布时间排序

      const mindSets = await this.service.index.getIndexData(app.config.indexModuleName[1], 1);
      //华渔视界去4条，发布时间排序
      console.log(mindSets);
      const fieldViews = await this.service.index.getIndexData(app.config.indexModuleName[2], 4);
      //产生访问记录
      const date = new Date();
      await this.service.visitLog.create({
        year: date.getFullYear(),
        create_time: date,
        visit_url: ctx.originalUrl,
        month: date.getMonth(),
        user_id: ctx.session.frontUser.user_id
      });
      
      ctx.locals = {
        bodyClass: 'home',
        banners: banners,
        announcements: announcements,
        mindSets: mindSets,
        fieldViews: fieldViews,
        pagetitle: '首页'
      }
      // ctx.body = ctx.locals;
      await ctx.render('front/index.html');
    }
    
  }
}
